package com.mijie.homi.dao.commom;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import com.mijie.core.IBatisDao;
import com.mijie.homi.domain.commom.Comment;

@Repository
public class CommentDao extends IBatisDao{

	private Logger logger = Logger.getLogger(CommentDao.class);
	
	public int postComment(Comment comment){
		return this.insert("postComment", comment);
	}
	
	public Comment getCommentById(String tableName,long id){
		HashMap<String,Object> parameter = new HashMap<String,Object>();
		parameter.put("tableName", tableName);
		parameter.put("id", id);
		return this.selectOne("getCommentById", parameter);
	}
	public int increNumOfReply(String tableName,long targetId){
		HashMap<String,Object> parameter = new HashMap<String,Object>();
		parameter.put("tableName", tableName);
		parameter.put("id", targetId);
		return  this.update("increNumOfReply", parameter);
	}
	
	public int decreNumOfReply(String tableName,long targetId){
		HashMap<String,Object> parameter = new HashMap<String,Object>();
		parameter.put("tableName", tableName);
		parameter.put("id", targetId);
		return this.update("decreNumOfReply", parameter);
	}
	
	public int deleteComment(String tableName,int reviewer,long id){
		HashMap<String,Object> parameter = new HashMap<String,Object>();
		parameter.put("tableName", tableName);
		parameter.put("reviewer", reviewer);
		parameter.put("id", id);
		return  this.delete("deleteComment", parameter);
	}
	
	public int deleteCommentByAdmin(String tableName,int administrator,long id){
		HashMap<String,Object> parameter = new HashMap<String,Object>();
		parameter.put("tableName", tableName);
		parameter.put("receiver", administrator);
		parameter.put("id", id);
		return this.delete("deleteCommentByAdmin", parameter);
	}
	
	public List<Comment> listComment(String tableName,long idx,long targetId,int limit){
		HashMap<String,Object> parameter = new HashMap<String,Object>();
		parameter.put("idx", idx);
		parameter.put("tableName", tableName);
		parameter.put("targetId", targetId);
		parameter.put("limit", limit);
		return this.selectList("listComment", parameter);
	}
	
	public void increNumOfComment(String tableName,long targetId){
		HashMap<String,Object> parameter = new HashMap<String,Object>();
		parameter.put("tableName", tableName);
		parameter.put("id", targetId);
		this.update("increNumOfComment",parameter);
	}
	
	public void decreNumOfComment(String tableName,long targetId){
		HashMap<String,Object> parameter = new HashMap<String,Object>();
		parameter.put("tableName", tableName);
		parameter.put("id", targetId);
		this.update("decreNumOfComment",parameter);
	}
	
}
